package com.example.howard.xiao7music.ui.fragment.LocalFrament;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.howard.xiao7music.MyApplication;
import com.example.howard.xiao7music.R;
import com.example.howard.xiao7music.presenter.LocalSongPresenter;
import com.example.howard.xiao7music.ui.Activity.LocalActivity.LocalSongActivity;
import com.example.howard.xiao7music.ui.BottomPlayer;
import com.example.howard.xiao7music.ui.adapter.LocalAdapter.LocalAllSongAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Howard on 2016/9/29.
 */
public class LocalAllSongFragment extends Fragment {
    private LocalSongPresenter localSongPresenter=new LocalSongPresenter();
    private List<Map<String, Object>> songlist=new ArrayList<>();
    private LocalAllSongAdapter myAdapter;
    private ListView listView;
    public AdapterView.OnItemClickListener listener;
    private MyApplication myApplication;
    private BottomPlayer bottomPlayer;
    private LocalSongActivity localSongActivity;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.local_all_song, null);
        listView= (ListView) view.findViewById(R.id.local_song_name_list);
        myApplication= (MyApplication) getActivity().getApplication();
        localSongActivity= (LocalSongActivity) this.getActivity();
        bottomPlayer=localSongActivity.getBottomplayer();
        songlist=localSongPresenter.getLocalAllSonglist(getContext());
        refreshListView();
        return view;
    }

    private void refreshListView(){
        myAdapter = new LocalAllSongAdapter(getContext(), songlist, new LocalAllSongAdapter.LocalMainCallback() {
            @Override
            public void delete(int position) {

                dialog(position);
            }

        });
        listView.setAdapter(myAdapter);
        listener= resetLickListener();
        listView.setOnItemClickListener(listener);
//        myAdapter.notifyDataSetChanged();
    }

    private AdapterView.OnItemClickListener resetLickListener(){
        AdapterView.OnItemClickListener songListclickListener = new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                myApplication.setSongdatalist(songlist);
                bottomPlayer.callByList(arg2);
            }
        };
        return songListclickListener;
    }


    protected void dialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("确认删除指定歌曲吗？");

        builder.setTitle("删除本地歌曲");

        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                File remove = new File(songlist.get(position).get("URL").toString());
                remove.delete();
                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri uri = Uri.fromFile(remove);
                intent.setData(uri);
                getContext().sendBroadcast(intent);
                dialog.dismiss();
                songlist.remove(position);
//                synchronized (Thread.currentThread()) {
//                    try {
//                        songlist.notify();
//                        myAdapter.notify();
//                        listView.notify();
//
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
                refreshListView();

            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        builder.create().show();
    }
}
